﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace AzukiMapUI
{
    public partial class ColorPallet : UserControl
    {
        string _colorPalletDataUri;
        bool _canRemove;

        public event SelectionChangedEventHandler SelectionChanged
        {
            add
            {
                ItemContainer.SelectionChanged += value;
            }
            remove
            {
                ItemContainer.SelectionChanged -= value;
            }
        }

        public string ColorPalletDataUri
        {
            set
            {
                if (FormInputControl.UriStringChecker(value))
                {
                    _colorPalletDataUri = value;
                    SetPalletFromXml(_colorPalletDataUri);
                }
            }
            get
            {
                return _colorPalletDataUri;
            }

        }

        public bool CanRemove
        {
            set { _canRemove = value; }
            get { return _canRemove; }
        }

        public ColorPallet()
        {
            InitializeComponent();
            _canRemove = false;
        }

        public void Add(Color c, string name, string nativeName)
        {
            PalletRect rect = new PalletRect(c.R, c.G, c.B, c.A);
            if (name == "")
            {
                rect.Name = c.ToString();
            }
            else
            {
                rect.Name = name;
            }
            if (nativeName == "")
            {
                rect.NativeName = c.ToString();
            }
            else
            {
                rect.NativeName = nativeName;
            }
            AddRect(rect);
        }

        void SetPalletFromXml(string uriStr)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ColorPalletXml_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri(uriStr));
        }

        void ColorPalletXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string res = e.Result;
                AddColorsFromXml(res);
            }
            else
            {
            }
        }

        void AddColorsFromXml(string resText)
        {
            XDocument iconsXml = XDocument.Parse(resText);
            IEnumerable<PalletRect> colors = from elem in iconsXml.Descendants("color")
                         select new PalletRect
                         { 
                             Number = (int)elem.Attribute("num"),
                             Name = (string)elem.Attribute("name"),
                             R = (byte)((int)elem.Attribute("r")),
                             G = (byte)((int)elem.Attribute("g")),
                             B = (byte)((int)elem.Attribute("b")),
                             A = (byte)((int)elem.Attribute("a")),
                             NativeName = (string)elem.Attribute("nativeName"),
                         };

            foreach (PalletRect color in colors)
            {
                AddRect(color);
            }
        }

        void AddRect(PalletRect pRect)
        {
            ItemContainer.Items.Add(pRect.Rect);
        }

        Rectangle CreateColorRect(PalletRect color)
        {
            Color c = Color.FromArgb(color.A, color.R, color.G, color.B);
            SolidColorBrush fill = new SolidColorBrush(c);

            Rectangle rect = new Rectangle();
            rect.Width = 16;
            rect.Height = 16;
            rect.Fill = fill;
            rect.Margin = new Thickness(2);
            rect.Stroke = new SolidColorBrush(Colors.Gray);
            rect.StrokeThickness = 0.5;

            return rect;
        }

        private void ItemContainer_KeyUp(object sender, KeyEventArgs e)
        {
            if (!_canRemove)
            {
                return;
            }

            Rectangle selectedRect = (Rectangle)((ListBox)ItemContainer).SelectedItem;
            if (e.Key == Key.Delete)
            {
                ItemContainer.Items.Remove(selectedRect);
            }
        }

    }

}
